﻿using DbToDocument.Core.LiteDB;
using LiteDB;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DbToDocument.Core.LiteDB
{
    /// <summary>
    /// 方法 扩展类
    /// </summary>
    public static class LiteDBExtension
    {
        /// <summary>
        /// 注册Lite服务
        /// </summary>
        /// <param name="service"></param>
        public static void AddLiteDBSevice(this IServiceCollection service)
        {
            service.AddScoped<ILiteDBService, LiteDBService>();
        }

        /// <summary>
        /// 
        /// 主要用于程序启动时 对数据库进行初始化
        /// </summary>
        /// <param name="app"></param>
        public static void UseLiteDB(this IApplicationBuilder app)
        {
            //打开数据库，没有的话自动创建 
            using (var db = new LiteDatabase(LiteDBConfig.DBConnectString))
            {
                //获取记录集，没有的话会自动创建
                var lst = db.GetCollection<Menu>("Menu");
                //模拟数据
                List<Menu> menus = new List<Menu>();
                Menu em = new Menu();
                em.Code = Guid.NewGuid().ToString();
                em.Name = "数据库列表";
                em.ParentId = 0;
                em.Icon = "el-icon-s-grid";
                em.Url = "/DataBase/Index";
                menus.Add(em);
                if (lst == null || lst.Count() == 0)
                {
                    //插入记录
                    lst.Insert(menus);
                }
            }
        }
    }
}
